//
//  MainSalesViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/2/28.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "MainSalesViewController.h"

#import "DetailsViewController.h"
#import "TodoHttpManager.h"
//model类解析
#import "MJExtension.h"
#import "MJEModleView.h"

@interface MainSalesViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    NSMutableArray *_zongArr;
    
 
    
    NSMutableArray *_titArr;

    NSString *louStr;
}


@property(nonatomic,strong)UITableView *tableView;

//model
@property (nonatomic,strong) NSMutableArray *daArr;
@property (nonatomic,strong) NSMutableArray *qishuArr;
@property(nonatomic,strong)NSDictionary *dic;

@end

@implementation MainSalesViewController

-(void)viewWillAppear:(BOOL)animated
{
    //主售房源通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDFZhuShouHouse:) name:TDFZhuShouHouse object:nil];
   
    if (_daArr.count == 0)
    {
        //TODO:主售房源
        [_manager userFYZhuShou];
    }
    
   
  
    
}
 //主售房源通知方法
-(void)didTDFZhuShouHouse:(NSNotification *)notifi
{
 
    if (notifi.object !=0 ) {
       for (NSDictionary *dict in notifi.object[@"data"])
       {
            [self.daArr addObject:[MJEModleView mj_objectWithKeyValues:dict]];
           
            [self.qishuArr addObject:dict[@"qishu"]];
           // [_titArr addObject:dict[@"block_num"]];
          // NSLog(@"rrrrrrrrr ==aaaaa返回的数据是 ==  %d",_daArr.count);
        
       }
        [self.tableView reloadData];
    }
    else
    {
        [_manager MakeMyselfAlert:@"正在加载" andIsNeedTitle:NO andShowTime:2.0];
    }

}
-(void)dealloc
{
     //移除主售房源通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDFZhuShouHouse object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
 
   
    _dic = [[NSDictionary alloc] init];
    _titArr = [NSMutableArray arrayWithCapacity:0];
    _manager = [TodoHttpManager sharedManager];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT-158) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 40;
    _tableView.backgroundColor = BgColor;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tableView];
    

    
}

#pragma mark--刷新---------刷新--------


//楼号数组 数组
-(NSMutableArray *)daArr{
    if (!_daArr) {
        _daArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _daArr;
}

//期数数据 数组
-(NSMutableArray *)qishuArr{
    if (!_qishuArr) {
        _qishuArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _qishuArr;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
     // NSLog(@"主售房源的数组长度  111==%ld",self.daArr.count);
    return self.daArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (_daArr.count != 0) {
        
        MJEModleView *model = self.daArr[section];
        
       // NSLog(@"0这里是什么呀--------%@",self.daArr[section]);
        return model.data.count;
        
    }
    
    return 0;
 
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifi = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifi];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifi];
        
    }

    cell.textLabel.textColor = OverColor;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    MJEModleView *model = self.daArr[indexPath.section];
   
    _dic = model.data[indexPath.row];
    
    NSString * blockStr = [NSString stringWithFormat:@"%@ 号楼",_dic[@"block_num"]];
    cell.textLabel.text = blockStr;
    cell.textLabel.textColor = [UIColor orangeColor];
    cell.detailTextLabel.textColor = [UIColor grayColor];
    return cell;
}
/*设置标题头的宽度*/
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

/*设置标题头的名称*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{


     return [self.qishuArr objectAtIndex:section];
}

/*设置cell 的宽度 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 40;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击的是哪个 %ld",(long)indexPath.row);
    
    MJEModleView *model = self.daArr[indexPath.section];
    louStr = [[model.data objectAtIndex:indexPath.row] objectForKey:@"block_num"];
    
    DetailsViewController *detaiVC = [[DetailsViewController alloc] init];
    
    detaiVC.proID =[[model.data objectAtIndex:indexPath.row] objectForKey:@"project_id"];
  
    detaiVC.zongStr = [[model.data objectAtIndex:indexPath.row] objectForKey:@"all_rooms"];
    detaiVC.yiStr = [[model.data objectAtIndex:indexPath.row] objectForKey:@"sale_rooms"];
    detaiVC.weiStr = [[model.data objectAtIndex:indexPath.row] objectForKey:@"rest_rooms"];
  
    [self.navigationController pushViewController:detaiVC animated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
